﻿@page "/"
@using Dependify.Core
@using Dependify.Core.Graph
@using Dependify.Core.Serializers
@using Microsoft.Extensions.Options
@using static Dependify.Core.SolutionRegistry

@inject SolutionRegistry SolutionRegistry
@inject IOptions<MsBuildConfig> MsBuildConfig
@inject IDialogService DialogService

<PageTitle>Dependify</PageTitle>

<MudToolBar>
    <MudText Typo="Typo.h4">Workbench ⚙️</MudText>
    <MudTooltip Text="Re-sync Solutions">
        <MudIconButton Icon="@Icons.Material.Filled.Sync" Size="Size.Medium" Color="Color.Primary" OnClick="() => LoadSolutionsAsync(true)" />
    </MudTooltip>
    <MudSpacer />
    <MudIconButton Icon="@Icons.Material.Outlined.PlayCircleFilled" Size="Size.Large" Color="Color.Primary" OnClick="@AnalyzeSolution" />
    <MudSelect T="string" AnchorOrigin="Origin.BottomCenter" AdornmentIcon="@Icons.Material.Filled.RocketLaunch"
        AdornmentColor="Color.Primary" @bind-Value="selectedSolution" Typo="Typo.h6" Label="Solution">
        <MudSelectItem T="string" />
        @foreach (var solutionNode in solutionNodes)
        {
            <MudSelectItem Value="@solutionNode.Id" />
        }
    </MudSelect>
</MudToolBar>

<MudContainer MaxWidth="MaxWidth.ExtraLarge">
    <MudPaper Class="pa-4 ma-16" Elevation="3">
        @if (displayMode == DisplayMode.All)
        {
            <MudSimpleTable Style="overflow-x: auto;" Dense="true" Hover="true" Bordered="true" Striped="true">
                <thead>
                    <tr>
                        <th><MudText Typo="Typo.h6">Name</MudText></th>
                        <th><MudText Typo="Typo.h6">Path</MudText></th>
                    </tr>
                </thead>
                <tbody>
                    @foreach (var node in nodes)
                    {
                        <tr>
                            <td>
                            @if (node.Node.Type == NodeConstants.Solution)
                            {
                                <MudText Color="Color.Secondary">@node.Node.Id</MudText>
                            }
                            else
                            {
                                <MudText Style="@($"color:{Colors.Teal.Lighten1}")">@node.Node.Id</MudText>
                            }
                            </td>
                            <td>
                                <MudText Style="@(node.Loaded ? $"color:{Colors.Green.Darken3}": string.Empty)">
                                    @node.Node.DirectoryPath.RemovePrefix(commonPrefix)
                                    @if(node.Loaded)
                                    {
                                        <MudIcon Icon="@Icons.Material.Filled.Check" Size="Size.Small" />
                                    }
                                </MudText>

                            </td>
                        </tr>
                    }
                </tbody>
            </MudSimpleTable>
        }
        else
        {
            <MudSimpleTable Style="overflow-x: auto;" Dense="true" Hover="true" Bordered="true" Striped="true">
                <thead>
                    <tr>
                        <th><MudText Style="@($"font-weight:bold")">Name</MudText></th>
                        <th><MudText Style="@($"text-align: center; font-weight:bold")">Depends On Projects</MudText></th>
                        <th><MudText Style="@($"text-align: center; font-weight:bold")">Used By Projects</MudText></th>
                        <th><MudText Style="@($"text-align: center; font-weight:bold")">Depends On Packages</MudText></th>
                        <th><MudText Style="@($"text-align: center; font-weight:bold")">Actions</MudText></th>
                    </tr>
                </thead>
                <tbody>
                    @foreach (var nodeUsage in nodeUsageStatistics)
                    {
                        <tr>

                            <td>
                                <MudTooltip>
                                    <ChildContent>
                                        @if (nodeUsage.Node.Type == NodeConstants.Solution)
                                        {
                                            <MudText Color="Color.Secondary">@nodeUsage.Node.Id</MudText>
                                        }
                                        else
                                        {
                                            <MudText Style="@($"color:{Colors.Teal.Lighten1}")">@nodeUsage.Node.Id</MudText>
                                        }
                                    </ChildContent>
                                    <TooltipContent>
                                        <MudText>@nodeUsage.Node.DirectoryPath</MudText>
                                    </TooltipContent>
                                </MudTooltip>
                            </td>
                            <td style="text-align: center;">
                                <MudTooltip>
                                    <ChildContent>
                                        <MudText Style="@($"color:{Colors.Teal.Lighten3}; font-weight:bold")">@nodeUsage.DependsOnProjectsCount</MudText>
                                    </ChildContent>
                                    <TooltipContent>
                                        @foreach (var childProject in nodeUsage.DependsOnProjects)
                                        {
                                            <MudText Style="@($"color:{Colors.Teal.Lighten3}")">@childProject.Id</MudText>
                                        }
                                    </TooltipContent>
                                </MudTooltip>
                            </td>
                            <td style="text-align: center;">
                                <MudTooltip>
                                    <ChildContent>
                                        <MudText Style="@($"color:{Colors.Teal.Lighten3}; font-weight:bold")">@nodeUsage.UsedByCount</MudText>
                                    </ChildContent>
                                    <TooltipContent>
                                        @foreach (var childProject in nodeUsage.UsedBy)
                                        {
                                            <MudText Style="@($"color:{Colors.Teal.Lighten3}")">@childProject.Id</MudText>
                                        }
                                    </TooltipContent>
                                </MudTooltip>
                            </td>
                            <td style="text-align: center;">

                                <MudTooltip>
                                    <ChildContent>
                                        <MudText Style="@($"color:{Colors.Lime.Darken3}; font-weight:bold")">@nodeUsage.DependsOnPackagesCount</MudText>
                                    </ChildContent>
                                    <TooltipContent>
                                        @foreach (var package in nodeUsage.DependsOnPackages)
                                        {
                                            <MudText Style="@($"color:{Colors.Lime.Darken3}")">@package.Id [@package.Version]</MudText>
                                        }
                                    </TooltipContent>
                                </MudTooltip>

                            </td>
                            <td style="@($"text-align: center")">
                                <MudTooltip Text="Open Graph Diagram">
                                    <MudIconButton
                                        Icon="@Icons.Material.TwoTone.FileOpen"
                                        Size="Size.Medium"
                                        Color="Color.Primary"
                                        OnClick="() => ShowDiagramModal(nodeUsage.Node.Id, DiagramStyle.Graph)"/>
                                </MudTooltip>

                                <MudTooltip Text="Open C4 Diagram">
                                    <MudIconButton
                                        Icon="@Icons.Material.Filled.FileOpen"
                                        Size="Size.Medium"
                                        Color="Color.Primary"
                                        OnClick="() => ShowDiagramModal(nodeUsage.Node.Id, DiagramStyle.C4)" />
                                </MudTooltip>

                                <MudTooltip Text="Copy JSON to Clipboard">
                                    <MudIconButton Icon="@Icons.Material.Filled.ContentCopy" Size="Size.Medium" Color="Color.Primary" OnClick="() => CopyDiagramToClipboard(nodeUsage.Node.Id)" />
                                </MudTooltip>

                            </td>
                        </tr>
                    }
                </tbody>
            </MudSimpleTable>
        }
    </MudPaper>
</MudContainer>
